﻿

<?php
include "connect/connect_dsccmis.php";
mysql_query("set names tis620");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Content-type: text/xml;  charset=TIS-620");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml");

createKmlFile();
function parseToXML($htmlStr)
{ 

$xmlStr=str_replace('<','&lt;',$htmlStr); 

$xmlStr=str_replace('>','&gt;',$xmlStr); 

$xmlStr=str_replace('"','&quot;',$xmlStr); 

$xmlStr=str_replace("'",'&#39;',$xmlStr); 

$xmlStr=str_replace("&",'&amp;',$xmlStr); 

return $xmlStr; 

} 


function createKmlFile(){

// //$OccNumber = $_GET["OccNumber"];
		// //if(document.getElementById("OccNumber").checked){	 OccNumber       	 =  "1";	}			else{	 OccNumber       	 =  "";	 				    }
		
		// //("occNumber1").checked){	 occNumber1      	 =  "1";	}	else{	 occNumber1      	 =  "";	 			    }
		// //("occNumber2").checked){	 occNumber2      	 =  "2";	}	else{	 occNumber2      	 =  "";	 			    }
		// //("occNumber3").checked){	 occNumber3      	 =  "3";	}	else{	 occNumber3      	 =  "";	 			    }
		// //("occNumber4").checked){	 occNumber4      	 =  "4";	}	else{	 occNumber4      	 =  "";				    }
// //		$OccNumberstr="";
// //		$Occ_Number = $_GET["Occ_Number"];
// //		$OccNumberzz = $_GET['OccNumber'];
// //		if($Occ_Number=="1"){
// //			$occNumber1 = $_GET["occNumber1"];
// //			$occNumber2 = $_GET["occNumber2"];
// //			$occNumber3 = $_GET["occNumber3"];
// //			$occNumber4 = $_GET["occNumber4"];
// //			if($occNumber1=="1") $OccNumberstr=$OccNumberstr.",'1'";
// //			if($occNumber2=="2") $OccNumberstr=$OccNumberstr.",'2'";
// //			if($occNumber3=="3") $OccNumberstr=$OccNumberstr.",'3'";
// //			if($occNumber4=="4") $OccNumberstr=$OccNumberstr.",'4'";
// //			$OccNumberstr=substr($OccNumberstr,1);
// //			$OccNumberstr=" and occ_num in (".$OccNumberstr.")";
// //		}
		// //("chkbox_project").checked){	 chkbox_project  	 =  "1";	}else{	 chkbox_project  	 =  "";	 			    }
		// //("chkbox_project1").checked){	 chkbox_project1 	 =  "1";	}else{	 chkbox_project1 	 =  "";		    }
		// //("chkbox_project2").checked){	 chkbox_project2 	 =  "2";	}else{	 chkbox_project2 	 =  "";	 		    }
		// //("chkbox_project3").checked){	 chkbox_project3 	 =  "3";	}else{	 chkbox_project3 	 =  "";	 		    }
		// //("chkbox_project4").checked){	 chkbox_project4 	 =  "4";	}else{	 chkbox_project4 	 =  "";	 			    }
		// //("chkbox_project5").checked){	 chkbox_project5 	 =  "5";	}else{	 chkbox_project5 	 =  "";	    }
		// $chkbox_projecstr="";
		// $chkbox_project = $_GET["chkbox_project"];
		// //$chkbox_projectxx = $_GET["chkbox_project"];
		// if($chkbox_project=="1")
		// {
			// $chkbox_project1 = $_GET["chkbox_project1"];
			// $chkbox_project2 = $_GET["chkbox_project2"];
			// $chkbox_project3 = $_GET["chkbox_project3"];
			// $chkbox_project4 = $_GET["chkbox_project4"];
			// $chkbox_project5 = $_GET["chkbox_project5"];
			// if($chkbox_project1=="1") $chkbox_projecstr=$chkbox_projecstr.",'1'";
			// if($chkbox_project2=="2") $chkbox_projecstr=$chkbox_projecstr.",'2'";
			// if($chkbox_project3=="3") $chkbox_projecstr=$chkbox_projecstr.",'3'";
			// if($chkbox_project4=="4") $chkbox_projecstr=$chkbox_projecstr.",'4'";
			// if($chkbox_project5=="5") $chkbox_projecstr=$chkbox_projecstr.",'5'";
			// $chkbox_projecstr=substr($chkbox_projecstr,1);
			// $chkbox_projecstr=" and occ_id in (".$chkbox_projecstr.")";
			
		// }
		// //$chkbox_projecstr=" and occ_id in ('1')";
		$occProvincestr = "";
		$occProvince = $_GET["OccProvince"];
		$occProvincexx = $_GET["OccProvince"];
		
			$occProvince90 = $_GET["occProvince90"];
			$occProvince94 = $_GET["occProvince94"];
			$occProvince95 = $_GET["occProvince95"];
			$occProvince96 = $_GET["occProvince96"];
			$occProvince00 = $_GET["occProvince00"];
			if($occProvince00!="00"){
			if($occProvince90=="90") $occProvincestr=$occProvincestr.",'สงขลา'";
			if($occProvince94=="94") $occProvincestr=$occProvincestr.",'ปัตตานี'";
			if($occProvince95=="95") $occProvincestr=$occProvincestr.",'ยะลา'";
			if($occProvince96=="96") $occProvincestr=$occProvincestr.",'นราธิวาส'";
			$occProvincestr=substr($occProvincestr,1);
			$occProvincestr=" and prv  in (".$occProvincestr.")";	
			}				
			else	if($occProvince90 == "90"   and
			           $occProvince94 == "94"   and
			           $occProvince95 == "95"   and
			           $occProvince96 == "96"
			
			
			)
			{
			$occProvincestr='';
			}
			else
			{
			if($occProvince90 != "90") $occProvincestr=$occProvincestr.",'สงขลา'";
			if($occProvince94 != "94") $occProvincestr=$occProvincestr.",'ปัตตานี'";
			if($occProvince95 != "95") $occProvincestr=$occProvincestr.",'ยะลา'";
			if($occProvince96 != "96") $occProvincestr=$occProvincestr.",'นราธิวาส'";
			$occProvincestr=substr($occProvincestr,1);
			$occProvincestr=" and prv not in (".$occProvincestr.")";
			}
			
		$occProvincexx = $occProvincestr;
		// $OccNumberstr="";
		// $OccNumber = $_GET["OccNumber"];		
		// $OccNumberzz = $_GET['OccNumber'];		
		// if($OccNumber=="1"){		
			// $occNumber1 = $_GET["occNumber1"];		
			// $occNumber2 = $_GET["occNumber2"];		
			// $occNumber3 = $_GET["occNumber3"];		
			// $occNumber4 = $_GET["occNumber4"];		
			// if($occNumber1=="1") $OccNumberstr=$OccNumberstr.",'1'";		
			// if($occNumber2=="2") $OccNumberstr=$OccNumberstr.",'2'";		
			// if($occNumber3=="3") $OccNumberstr=$OccNumberstr.",'3'";		
			// if($occNumber4=="4") $OccNumberstr=$OccNumberstr.",'4'";		
			// $OccNumberstr=substr($OccNumberstr,1);		
			// $OccNumberstr=" and occ_num in (".$OccNumberstr.")";		
		// }		
		// //("OccProvince").checked){	OccProvince	 		=  "1";	}else{	OccProvince	 =  "";						    }
		// //("occProvince90").checked){	occProvince90    	 =  "90";	}else{	occProvince90    	 =  "";				    }
		// //("occProvince94").checked){	occProvince94    	 =  "94";	}else{	occProvince94    	 =  "";				    }
		// //("occProvince95").checked){	occProvince95    	 =  "95";	}else{	occProvince95    	 =  "";				    }
		// //("occProvince96").checked){	occProvince96    	 =  "96";	}else{	occProvince96    	 =  "";				    }


		
		
		
		
		// //("chk_occ").checked){	chk_occ          	 =  "1";	}else{	chk_occ          	 =  "";				    }
		// //("chk_occ1").checked){	chk_occ1         	 =  "ขายอาหาร"			;		}else{	chk_occ1         	 =  "";				    }
		// //("chk_occ2").checked){	chk_occ2         	 =  "ขายของชำ"		;		}else{	chk_occ2         	 =  "";				    }
		// //("chk_occ3").checked){	chk_occ3         	 =  "ตัดเย็บ"		;		}else{	chk_occ3         	 =  "";				    }
		// //("chk_occ4").checked){	chk_occ4         	 =  "เลี้ยงสัตว์"		;		}else{	chk_occ4         	 =  "";				    }
		// //("chk_occ5").checked){	chk_occ5         	 =  "ทำสวน/ปลูกผัก"	;	}else{	chk_occ5         	 =  "";			    }
		// //("chk_occ6").checked){	chk_occ6         	 =  "ร้านเสริมสวย"	;	}else{	chk_occ6         	 =  "";			    }
		// //("chk_occ7").checked){	chk_occ7         	 =  "อื่น ๆ"			;	}else{	chk_occ7         	 =  "";	                            }
		// $chk_occstr="";
		// $chk_occ = $_GET["chk_occ"];           
		// if($chk_occ=="1"){                     
			// $chk_occ1 = $_GET["chk_occ1"];     
			// $chk_occ2 = $_GET["chk_occ2"];     
			// $chk_occ3 = $_GET["chk_occ3"];     
			// $chk_occ4 = $_GET["chk_occ4"];     
			// $chk_occ5 = $_GET["chk_occ5"];     
			// $chk_occ6 = $_GET["chk_occ6"];     
			// $chk_occ7 = $_GET["chk_occ7"];
			// if($chk_occ1== "ขายอาหาร") $chk_occstr=$chk_occstr.",'ขายอาหาร'";
			// if($chk_occ2== "ขายของชำ") $chk_occstr=$chk_occstr.",'ขายของชำ'";
			// if($chk_occ3== "ตัดเย็บ") $chk_occstr=$chk_occstr.",'ตัดเย็บ'";
			// if($chk_occ4== "เลี้ยงสัตว์") $chk_occstr=$chk_occstr.",'เลี้ยงสัตว์'";
			// if($chk_occ5== "ทำสวน/ปลูกผัก") $chk_occstr=$chk_occstr.",'ทำสวน/ปลูกผัก'";
			// if($chk_occ6== "ร้านเสริมสวย") $chk_occstr=$chk_occstr.",'ร้านเสริมสวย'";
			// if($chk_occ7== "อื่น ๆ") $chk_occstr=$chk_occstr.",'อื่น ๆ'";
			
			// $chk_occstr=substr($chk_occstr,1);
			// $chk_occstr=" and type_occ_a in (".$chk_occstr.")";
		// }

//$xxx=$chk_occstr.$occProvincestr.$chkbox_projecstr.$OccNumberstr;
//$query = "select * from occ "."where lat is not null and lng is not null".$chk_occstr.$occProvincestr.$chkbox_projecstr.$OccNumberstr." ORDER BY `occ`.`id_adr_b`  DESC";
//$query = "select * from victims_6868 where lat is not null and lng is not null and prv in ('ปัตตานี')  ORDER BY `victims_6868`.`plcidgen_mis` DESC ";
$query = "select * from victims_6868 where lat is not null and lng is not null".$occProvincestr." ORDER BY `victims_6868`.`plcidgen_mis` DESC ";
$queryA = "select * from gis_victim where lat is not null and lng is not null ORDER BY `gis_victim`.`prov_name` DESC ";
//$query="select * from victims_6868 where lat is not null and lng is not null and prv in (all,'ปัตตานี','ยะลา' ,not 'นราธิวาส') ORDER BY `victims_6868`.`plcidgen_mis` DESC ";
//echo $occProvincestr;
//<script type="text/javascript">
//</script>
mysql_query("SET NAMES UTF8");
$result = mysql_query($query);
$resultA = mysql_query($queryA);
if (!$result) {

  die('Invalid query: ' . mysql_error());

}
if (!$resultA) {

  die('Invalid query: ' . mysql_error());

}


header("Content-type: text/xml");


// Start XML file, echo parent node
//echo $query ;
echo '<farm>';

while ($row = @mysql_fetch_assoc($resultA)){
}
// Iterate through the rows, printing XML nodes for each
$id_adr_B=0;
$count=0;
$prv="ปัตตานี";
while ($row = @mysql_fetch_assoc($result)){
//$query1="select * from occ where id_adr_b=".$row['id_adr_b'];
//$result1 = mysql_query($query1);
//while ($row1 = @mysql_fetch_assoc($result1)){
  // ADD TO XML DOCUMENT NODE
//echo $query ;
//echo $xxx;
//echo $OccNumberzz;
//$id_adr_A= $row['id_adr_b'];
$id_adr_A= $row['plcidgen_mis'];
//$query1="SELECT COUNT(id_adr_b) FROM occ WHERE id_adr_b='".$row['id_adr_b']."'";
//$query1="SELECT COUNT(plcidgen_mis) FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'";
$query1="SELECT * FROM victims_6868 WHERE plcidgen_mis='".$row['plcidgen_mis']."'";
$result1 = mysql_query($query1);
//$row1 = @mysql_fetch_assoc($result1)
//$id_adr_B=$id_adr_A;
$num=mysql_num_rows($result1);
if($id_adr_B != $id_adr_A){
$id_adr_B = $id_adr_A;
  echo '<marker ';

// echo 'id="' . 			parseToXML($row['occ_name']) . '" ';
// echo 'idfarm="' . 		parseToXML($row['type_occ_a']) . '" ';
// echo 'surnme="' .	 	parseToXML($row['occ_day']) . '" ';
// echo 'prenme="' . 		parseToXML($row['D']) . '" ';
echo 'count="' . 			parseToXML($num) . '" ';

echo 'name="' . 			parseToXML($row['vll']) . '" ';
echo 'age="' . 	     		parseToXML($row['amp']) . '" ';
if("ปัตตานี"==parseToXML($row['prv']) or"ยะลา"==parseToXML($row['prv']) or"สงขลา"==parseToXML($row['prv']) or"นราธิวาส"==parseToXML($row['prv']))
{
echo 'address="'  . 'ม.'.parseToXML($row['vll']) .' ต.'.parseToXML($row['tmb']).' อ.'.parseToXML($row['amp']) .' จ.'.	parseToXML($row['prv']) . '" ';
}
else if("กรุงเทพมหานคร"==parseToXML($row['prv']) )
echo 'address="' . 	     		parseToXML($row['prv']) . '" ';
else
echo 'address="' . 	     		' จังหวัด'.parseToXML($row['prv']) . '" ';
//echo 'count="' . 		parseToXML($row1['occ_day']) . '" ';
  echo 'lat="' . 			$row['lat'] . '" ';

  echo 'lng="' . 			$row['lng'] . '" ';
//  echo 'type="' . 			$row['H'] . '" ';

  echo '/>';
  $count=0;
 // }
}else
$count =$count+1;
}


// End XML file
echo $query."\n";
echo $id_adr_A."\n";
echo $occProvincestr;
//echo $num;
echo '</farm>';
//echo $occProvincestr;
//echo $query ;


}

?>
